import usb1,threading
class LcopaUsb:
    def __init__(self):

        self.is_open = False
        self.context = usb1.USBContext()
        self.open_device=None
    def open(self,product_id):
        self.product_id = product_id
        print "product_id%04x:"%self.product_id
        #self.open_device = None
        for device in self.context.getDeviceList(skip_on_error=True):
            print 'ID %04x:%04x' % (device.getVendorID(), device.getProductID()), '->'.join(str(x) for x in ['Bus %03i' % (device.getBusNumber(), )] + device.getPortNumberList()), 'Device', device.getDeviceAddress()
        for device in self.context.getDeviceList(skip_on_error=True):
            if(device.getProductID() == self.product_id):
                self.usb_handle = device.open()
                #self.device = device
                self.open_device = device
                self.is_open = True
                print "device open\r\n"
        if self.is_open == False:
            print ("device not found:%04x\r\n"%self.product_id)
                #        break;
                #self.hotplug_del = threading.Thread(target=self.hotplug_del_thread)
                #self.hotplug = threading.Thread(target=self.hotplug_thread)
        # self.hotplug_del.start()
        # self.hotplug.start()
        # if self.is_open == False:
        #     if not self.context.hasCapability(usb1.CAP_HAS_HOTPLUG):
        #         print 'Hotplug support is missing. Please update your libusb version.'
        #         return
        #     opaque = self.context.hotplugRegisterCallback(self.hotplug_callback)
        #     try:
        #         while True:
        #             self.context.handleEvents()
        #     except (KeyboardInterrupt, SystemExit):
        #         pass
        # else:
        #     opaque = self.context.hotplugDeregisterCallback(self.hotplug_del_callback)
    def get_usb_list(self):
        usb_list=[]
        for device in self.context.getDeviceList(skip_on_error=True):
            print 'ID %04x:%04x' % (device.getVendorID(), device.getProductID()), '->'.join(str(x) for x in ['Bus %03i' % (device.getBusNumber(), )] + device.getPortNumberList()), 'Device', device.getDeviceAddress()
        for device in self.context.getDeviceList(skip_on_error=True):
            usb_list.append("%04x"%device.getProductID())
        return usb_list
    def close(self):
        if(self.open_device != None):
            self.open_device.close()

    def send_byte(self,byte_data):
        if(self.is_open == True):
            self.usb_handle.bulkWrite(0x02,byte_data,0)
        else:
            pass
    def hotplug_callback(self,context, device, event):
        print "inster usb\r\n"
        for device in self.context.getDeviceList(skip_on_error=True):
            if(device.getProductID() == self.product_id):
                self.usb_handle = device.open()
                self.is_open = True
                break;
    def hotplug_del_callback(self,context, device, event):

        print "delete usb\r\n"
        self.is_open = False

    def hotplug_del_thread(self):
        if not self.context.hasCapability(usb1.CAP_HAS_HOTPLUG):
            print 'Hotplug support is missing. Please update your libusb version.'
            return
        opaque = self.context.hotplugDeregisterCallback(self.hotplug_del_callback)
        try:
            while True:
                self.context.handleEvents()
        except (KeyboardInterrupt, SystemExit):
            pass

    def hotplug_thread(self):
        while(True):
            if not self.context.hasCapability(usb1.CAP_HAS_HOTPLUG):
                print 'Hotplug support is missing. Please update your libusb version.'
                return
            opaque = self.context.hotplugRegisterCallback(self.hotplug_callback)
            try:
                while True:
                    self.context.handleEvents()
            except (KeyboardInterrupt, SystemExit):
                pass

